#ifndef __SERVENT_H__
#define __SERVENT_H__

#include "../nodecache/nodecache.h"
#include "net_messages.h"
#include "../http/http_elements.h"
#include "peerlist.h"
#include "../parameters/parameter_struct.h"

http_messages	broadcast_cache;
http_model	*broadcast_model;
pthread_mutex_t	broadcast_mtx;

//funzione di inizializzazione
void servent_init();

//funzione di gestione delle richieste (parte server)
void net_http_server(node*);
//funzione di client, utilizzata principalmente per il debug
void net_http_client(node*);

//utilizzata per lo scambio del primo pacchetto di connessione
pkt_connect* net_get_connection(connection *c);

void client_ping_all();

//funzioni relative all'applicazione
int chat_notify_status(char *status);
int chat_get_list(char *chat_name);
void chat_send_message(char *, char*);
void chat_leave(char *chat_name);
void chat_join(char *chat_name);


static http_model connect_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_CONNECT,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 sizeof(pkt_connect),sizeof(pkt_connect),
 HTTP_STATUS_CONNECTED
};

static http_model disconnect_m = {
	HTTP_RQST_METHOD_GET,
 NETWORK_RQST_METHOD_DISCONNECT,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 0,0,
 HTTP_STATUS_DISCONNECTED
};



static http_model pong_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_PONG,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 34,0,
 HTTP_STATUS_NOCHANGE
};

static http_model message_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_MESSAGE,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 0,0,
 HTTP_STATUS_NOCHANGE
};

static http_model status_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_STATUS,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 sizeof(pkt_header),0,
 HTTP_STATUS_NOCHANGE
};

static http_model join_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_JOIN,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 sizeof(pkt_header),0,
 HTTP_STATUS_NOCHANGE
};

static http_model leave_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_LEAVE,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 sizeof(pkt_header),0,
 HTTP_STATUS_NOCHANGE
};

static http_model listhit_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_LISTHIT,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 sizeof(pkt_header),0,
 HTTP_STATUS_NOCHANGE
};

static http_model list_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_LIST,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 sizeof(pkt_header),0,
 HTTP_STATUS_NOCHANGE,
 &listhit_m
};

static http_model ping_m = {
	HTTP_RQST_METHOD_POST,
 NETWORK_RQST_METHOD_PING,
 HTTP_VERSION_TYPE,
 HTTP_RSPN_CODE_200,
 18,0,
 HTTP_STATUS_NOCHANGE
};

typedef struct {
	node *node;
	http_messages messages;
	http_model *model;
} http_forward_p;

void client_forward(void *);
void client_flood_forward(void *);
void client_forward_thread(node *, http_messages *, http_model *);
void client_flood_forward_thread(node *, http_messages *, http_model *);
void client_disconnect_all();

#define lock_broadcast()	pthread_mutex_lock(&broadcast_mtx)
#define unlock_broadcast()	pthread_mutex_unlock(&broadcast_mtx)

#include "net_messages.h"
			 
#endif